package com.yipao.wx.web.operate;

import com.yipao.core.util.ResponseUtil;
import com.yipao.core.validator.Order;
import com.yipao.core.validator.Sort;
import com.yipao.db.domain.devinfo.DevInfo;
import com.yipao.db.service.devinfo.DevGoodsService;
import com.yipao.db.service.devinfo.DevInfoService;
import com.yipao.wx.vo.DevGoodsNumVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author voyboy
 */
@RestController
@RequestMapping("/wx/operate")
@Validated
@Slf4j
public class WxOperateController {
    @Resource
    private DevInfoService devInfoService;

    @Resource
    private DevGoodsService devGoodsService;


    /**
     * 查询
     * @param devId 设备id
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    public Object getDevInfo(@RequestParam(value = "devId",required = false) Integer devId,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit,
                             @Sort @RequestParam(defaultValue = "add_time") String sort,
                             @Order @RequestParam(defaultValue = "desc") String order){
        List<DevGoodsNumVo> voList=new ArrayList<>();
        List<DevInfo> devInfos = devInfoService.selectByDevId(devId, page, limit, sort, order);
        for (DevInfo devInfo : devInfos) {
            DevGoodsNumVo vo=new DevGoodsNumVo();
            vo.setDevInfo(devInfo);
            vo.setGoodsNum(devGoodsService.findNumByDevId(devInfo.getId()+""));
            voList.add(vo);
        }

        return ResponseUtil.ok(voList);
    }

}
